/**
 * 
 */
package array.passed;

/**
 * @author xyyi
 *
 */
public class MergeSortedArray {

	/**
	Merge Sorted ArrayMay 20 '121869 / 4026

	Given two sorted integer arrays A and B, merge B into A as one sorted array.

	Note:
	You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
	 */
	public void merge(int A[], int m, int B[], int n) {

		int size = m + n - 1;
		m--;
		n--;
		while (m >= 0 && n >= 0) {
			if (A[m] > B[n]) {
				A[size--] = A[m--];
			} else {
				A[size--] = B[n--];
			}
		}

		while (m >= 0) {
			A[size--] = A[m--];
		}
		while (n >= 0) {
			A[size--] = B[n--];
		}

	}

	/**
	 * 
	 */
	public MergeSortedArray() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MergeSortedArray msa = new MergeSortedArray();
		int[] A = new int[] { 1, 0 };
		int[] B = new int[] { 2 };
		msa.merge(A, 1, B, 1);

	}
}
